'use strict';

App.controller('dashboardController', ['$rootScope', '$scope', '$state', 'Page','$interval', function($rootScope, $scope, $state, Page,$interval) {
	var param = {};
	param.pageIndex = 0;
	param.pageSize = 10;
    Page.ajaxPost('anon/www/bid/query.do',param,function(resp){
    	$scope.bidArray = resp.data.dataArray;
    	// 调用分页
    	$scope.totalPages = resp.data.totalPages;
		laypage({
			cont : 'page_bar',
			pages : $scope.totalPages,// 得到总页数
			skin : '#3E97E4',
			skip : true, // 是否开启跳页
			prev : '<', // 若不显示，设置false即可
			next : '>', // 若不显示，设置false即可
			groups : 5, //连续分页数
			curr: 1, //初始化当前页
			jump : function(obj,first) {// 触发分页后的回调
				/*obj是一个object类型。包括了分页的所有配置信息。
				 first一个Boolean类，检测页面是否初始加载。非常有用，可避免无限刷新。*/
				if(!first){
					//var param = {};
					param.pageIndex = obj.curr - 1;
					Page.ajaxPost('anon/www/bid/query.do', param, function(resp) {
						$scope.bidArray = resp.data.dataArray;
						$scope.totalPages = resp.data.totalPages;
					});
				}
			}
		})
    });
    
    var interval = 60000; // 一分钟
    //window.setInterval(function(){$scope.ShowCountDown();}, interval); 
    
    $scope.ShowCountDown = function() { 
		    for(var i=0;i<$scope.bidArray.length;i++){
		    	 var limitTime = $scope.bidArray[i].limitTime;
		    	 var endDate = new Date(limitTime); 
		    	 var now = new Date(); 
		         var leftTime=endDate.getTime()-now.getTime(); 
		         var leftsecond = parseInt(leftTime/1000); // 剩下秒数
		         var day=Math.floor(leftsecond/(60*60*24)); 
		         var hour=Math.floor((leftsecond-day*24*60*60)/3600); 
		         var minute=Math.floor((leftsecond-day*24*60*60-hour*3600)/60); 
		         //var second=Math.floor(leftsecond-day*24*60*60-hour*3600-minute*60); 
		         $scope.bidArray[i].limitTimeCountStr = "";
		         if(day>0){
		        	 $scope.bidArray[i].limitTimeCountStr += day+"天" 
	        		 $scope.bidArray[i].limitTimeCountStr += hour+"小时" 
		         }else{
		        	 if(hour>0){
		        		 $scope.bidArray[i].limitTimeCountStr += hour+"小时" 
		        	 }
		         }
		         $scope.bidArray[i].limitTimeCountStr +=minute+"分"; 
     		}
		    $scope.$apply($scope.bidArray);
        } 
}]);